import json
import requests
import pandas as pd
from os import listdir




def get_infos(s_v, a_v):
    List = []
    for t in range(1, 4):
        for p in range(1, 100):
            url = f'https://static-data.eol.cn/www/2.0/schoolprovinceindex/detial/{s_v}/{a_v}/{t}/{p}.json'
            response = requests.get(url)
            try:
                data = json.loads(response.text)
            except:
                return get_infos(s_v, a_v)
            if len(data) > 0:
                List += data['data']['item']
            else:
                break
    if len(List) != 0:
        data = pd.DataFrame(List)
        year_li = list(set(data['year']))
        dic = {}
        for year in year_li:
            value = data.loc[data['year'] == year].set_index('type').T.to_dict()
            dic[year] = value
        return dic
    else:
        return 0


if __name__ == '__main__':
    with open('./分组/1.json', 'r', encoding='utf-8') as f:
        school_id = f.read()
        school_id = json.loads(school_id)
    with open('地区_id.json', 'r', encoding='utf-8') as f:
        area_id = f.read()
        area_id = json.loads(area_id)
    for s_k, s_v in school_id.items():
        Map = {}
        if f'{s_k}.json' not in listdir('./save/'):
            dic = {}
            for a_k, a_v in area_id.items():
                d = get_infos(s_v, a_v)
                if d != 0:
                    dic[a_k] = d
                print(f'{s_k}  {a_k}   finish')
            Map[s_k] = dic
            Map['school_name'] = s_k
            with open(f'./save/{s_k}.json', 'w', encoding='utf-8') as f:
                json.dump(Map, f, ensure_ascii=False)


